﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get SubcommitteeStudent --

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<SubcommitteeStudent> GetStudentBySubcommitteeId(int subcommitteeId)
        {
            return DataContex != null && subcommitteeId > 0
                        ? DataContex.SubcommitteeStudents.Where(m => m.IdSubcommittee == subcommitteeId).ToList() : null;
        }

        public List<SubcommitteeStudent> GetSubcommitteeStudentByMajorId(int majorId)
        {
            return DataContex != null && majorId > 0
                        ? DataContex.SubcommitteeStudents.Where(m => m.Student.IdMajor == majorId).ToList()
                        : null;
        }

        #endregion

        #region -- Add Subcommittee Student--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommitteeStudent"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddSubcommitteeStudentValidator(SubcommitteeStudent subcommitteeStudent, out string message)
        {

            if (DataContex.SubcommitteeStudents.Any(f => f.IdSubcommittee == subcommitteeStudent.IdSubcommittee && f.IdStudent == subcommitteeStudent.IdStudent))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Sinh viên thuộc tiểu ban");
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddSubcommitteeStudent(SubcommitteeStudent subcommitteeStudent)
        {
            DataContex.SubcommitteeStudents.AddObject(subcommitteeStudent);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Subcommittee Students --


        public bool EditSubcommitteeStudentsValidator(SubcommitteeStudent subcommitteeStudent, out string message)
        {
            var id = subcommitteeStudent.Id;

            if (DataContex.SubcommitteeStudents.Any(f => f.IdSubcommittee == subcommitteeStudent.IdSubcommittee && f.IdStudent == subcommitteeStudent.IdStudent && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Sinh viên thuộc tiểu ban");
                return false;
            }
            message = string.Empty;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommitteeStudent"></param>
        /// <returns></returns>
        public bool EditSubcommitteeStudent(SubcommitteeStudent subcommitteeStudent)
        {
            var id = subcommitteeStudent.Id;
            var updateSubcommitteeStudent = DataContex.SubcommitteeStudents.SingleOrDefault(f => f.Id == id);

            if (updateSubcommitteeStudent == null)
                return false;

            updateSubcommitteeStudent.IdSubcommittee = subcommitteeStudent.IdSubcommittee;
            updateSubcommitteeStudent.IdStudent = subcommitteeStudent.IdStudent;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Subcommittee Student --

        public bool DeleteSubcommitteeStudentsValidator(IEnumerable<SubcommitteeStudent> subcommitteeStudents, out string message)
        {
            const string intialize = "Sinh viên thuộc tiểu ban: ";
            var itemsCannotIdentify = (subcommitteeStudents.Select(student => new { student, id = student.Id }).Where(
                 @t => !DataContex.SubcommitteeStudents.Any(m => m.Id == @t.id)).Select(@t => @t.student)).Aggregate(intialize, (current, student) => current + (student.IdSubcommittee + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteSubcommitteeStudents(List<SubcommitteeStudent> subcommitteeStudents)
        {
            for (var i = subcommitteeStudents.Count - 1; i >= 0; i--)
            {
                var id = subcommitteeStudents[i].Id;
                var deleteSubcommittee = DataContex.SubcommitteeStudents.Single(m => m.Id == id);
                DataContex.SubcommitteeStudents.DeleteObject(deleteSubcommittee);
            }
            DataContex.SaveChanges();
        }

        #endregion

    }
}